package kr.co.goodwilldd.framework.common.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import kr.co.goodwilldd.framework.util.PagingUtil;

/**
 * Common Paging
 * 
 * <pre>
 * Using
 * 	CommonPaging<SomethingEntity> page = CommonPaging<SomethingEntity>;
 * 	SomethingEntity param = new SomethingEntity();
 * 	page.getParamBean(param);
 * 	page.setList(somethingRepository.getList());
 * 	page.setCurrentPage(currentPage);
 * page.setPagingUtil(new PagingUtil(currentPage, totalListCount));
 * </pre>
 * 
 * @author GoodwillDD (kr.goodwilldd@gmail.com)
 * 
 * @param <T>
 */
@Component
public class CommonPaging<T> implements Serializable {

	@Value("#{board['page.list.count']}")
	int viewRow;

	@Value("#{board['page.list.block']}")
	int block_size;
	
	private static final long serialVersionUID = 3663714518297256443L;
	List<T> list;
	ArrayList<Map<String, String>> searchParams;
	int currentPage;
	PagingUtil pagingUtil;
	int boardSeq;

	public CommonPaging() {
		searchParams = new ArrayList<Map<String, String>>();
		pagingUtil = new PagingUtil(viewRow, block_size);
	}

	public void addParam(String key, String value) {
		this.searchParams.add(Collections.singletonMap(key, value));
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getCurrentPage() {
		return currentPage;
	}
	
	public void setCurrentPage(int currentPage) {
		this.currentPage = (currentPage - 1) * viewRow;
		this.pagingUtil.setCurrentPageNum(currentPage);
	}

	public PagingUtil getPagingUtil() {
		return pagingUtil;
	}

	public void setPagingUtil(PagingUtil pagingUtil) {
		this.pagingUtil = pagingUtil;
	}

	public ArrayList<Map<String, String>> getSearchParams() {
		return searchParams;
	}

	public void setSearchParams(ArrayList<Map<String, String>> searchParams) {
		this.searchParams = searchParams;
	}

	public int getBoardSeq() {
		return boardSeq;
	}

	public void setBoardSeq(int boardSeq) {
		this.boardSeq = boardSeq;
	}

}
